{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文件复制完成！\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import shutil\n",
    "\n",
    "# 源目录\n",
    "source_dir = 'D:\\\\fanxinlong\\\\铁通2402'\n",
    "# 目标目录\n",
    "target_dir = r'd:\\fan2402'\n",
    "\n",
    "# 确保目标目录存在\n",
    "if not os.path.exists(target_dir):\n",
    "    os.makedirs(target_dir)\n",
    "\n",
    "# 遍历源目录及其子目录\n",
    "for root, dirs, files in os.walk(source_dir):\n",
    "    for file in files:\n",
    "        # 构建完整的文件路径\n",
    "        source_file_path = os.path.join(root, file)\n",
    "        # 构建目标文件路径\n",
    "        target_file_path = os.path.join(target_dir, file)\n",
    "        \n",
    "        # # 如果目标文件已存在，添加一个数字后缀\n",
    "        # if os.path.exists(target_file_path):\n",
    "        #     base, extension = os.path.splitext(file)\n",
    "        #     counter = 1\n",
    "        #     while os.path.exists(os.path.join(target_dir, f\"{base}({counter}){extension}\")):\n",
    "        #         counter += 1\n",
    "        #     target_file_path = os.path.join(target_dir, f\"{base}({counter}){extension}\")\n",
    "        \n",
    "        # 复制文件\n",
    "        shutil.copy2(source_file_path, target_file_path)\n",
    "        print(f\"复制了: {source_file_path} 到 {target_file_path}\")\n",
    "\n",
    "print(\"文件复制完成！\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
